﻿using System;
using System.Collections.Generic;
using Opalis.QuickIntegrationKit;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using System.Text;
using System.Net;

namespace ExchangeMail
{
    [OpalisObject("Send Email",ShowFilters=false)]
    public class SendEmail : IOpalisObject
    {
        private MailboxSettings settings;
        private String exchangeVersion = String.Empty;

        private String userName = String.Empty;
        private String password = String.Empty;
        private String domain = String.Empty;
        private String serviceURL = String.Empty;

        private String ToRecipients = String.Empty;
        private String Subject = String.Empty;
        private String Body = String.Empty;
        private String Attachements = String.Empty;
        private String ImportanceLevel = String.Empty;


        [OpalisConfiguration]
        public MailboxSettings Settings
        {
            get { return settings; }
            set { settings = value; }
        }


        public void Design(IOpalisDesigner designer)
        {
            string[] importanceOptions = new string[3];
            importanceOptions[0] = "High";
            importanceOptions[1] = "Low";
            importanceOptions[2] = "Normal";

            designer.AddInput("Subject");
            designer.AddInput("Recipients");
            designer.AddInput("Body");
            designer.AddInput("Importance Level").WithListBrowser(importanceOptions).WithDefaultValue("Normal");
            designer.AddInput("Attachment File Locations").NotRequired().WithFileBrowser();

            designer.AddOutput("EmailSubject").AsString();
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {
            userName = settings.UserName;
            password = settings.Password;
            domain = settings.Domain;
            serviceURL = settings.ServiceUrl;
            exchangeVersion = settings.ExchangeVersion;

            ToRecipients = request.Inputs["Recipients"].AsString();
            Subject = request.Inputs["Subject"].AsString();
            Body = request.Inputs["Body"].AsString();
            Attachements = request.Inputs["Attachment File Locations"].AsString();
            ImportanceLevel = request.Inputs["Importance Level"].AsString();

            ExchangeService service = new ExchangeService();
            switch (exchangeVersion)
            {
                case "Exchange2007_SP1":
                    service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                    break;
                case "Exchange2010":
                    service = new ExchangeService(ExchangeVersion.Exchange2010);
                    break;
                case "Exchange2010_SP1":
                    service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
                    break;
                default:
                    service = new ExchangeService();
                    break;
            }

            service.Credentials = new WebCredentials(userName, password, domain);
            String AccountUrl = userName + "@" + domain;

            if (serviceURL.Equals("Autodiscover")) { service.AutodiscoverUrl(AccountUrl); }
            else { service.Url = new Uri(serviceURL); }

            EmailMessage message = new EmailMessage(service);

            foreach(String recipient in ToRecipients.Split(';'))
            {
                message.ToRecipients.Add(recipient);
            }

            message.Subject = Subject;
            message.Body = Body;

            if (Attachements != String.Empty)
            {
                foreach (String AttachmentPath in Attachements.Split(';'))
                {
                    message.Attachments.AddFileAttachment(AttachmentPath);
                }
            }

            switch (ImportanceLevel)
            {
                case "High":
                    message.Importance = Importance.High;
                    break;
                case "Low":
                    message.Importance = Importance.Low;
                    break;
                case "Normal":
                    message.Importance = Importance.Normal;
                    break;
                default:
                    message.Importance = Importance.Normal;
                    break;
            }

            message.Send();

            response.Publish("EmailSubject", message.Subject.ToString());
        }
    }
}
